package com.zxy.designpattern.observerpattern;

import java.util.HashMap;

public class SimpleContext implements Lifecycle {

  public SimpleContext() {
  }

  protected HashMap children = new HashMap();
  protected LifecycleSupport lifecycle = new LifecycleSupport(this);
  private HashMap servletMappings = new HashMap();
  protected HashMap mappers = new HashMap();
  protected boolean started = false;

  // implementation of the Lifecycle interface's methods
  public void addLifecycleListener(LifecycleListener listener) {
    lifecycle.addLifecycleListener(listener);
  }

  public LifecycleListener[] findLifecycleListeners() {
    return null;
  }

  public void removeLifecycleListener(LifecycleListener listener) {
    lifecycle.removeLifecycleListener(listener);
  }

  public synchronized void start() throws LifecycleException {
    if (started)
      throw new LifecycleException("SimpleContext has already started");

    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
    started = true;
    // try {
    // // Start our subordinate components, if any
    // if ((loader != null) && (loader instanceof Lifecycle))
    // ((Lifecycle) loader).start();
    //
    // // Start our child containers, if any
    // Container children[] = findChildren();
    // for (int i = 0; i < children.length; i++) {
    // if (children[i] instanceof Lifecycle)
    // ((Lifecycle) children[i]).start();
    // }
    //
    // // Start the Valves in our pipeline (including the basic),
    // // if any
    // if (pipeline instanceof Lifecycle)
    // ((Lifecycle) pipeline).start();
    // // Notify our interested LifecycleListeners
    // lifecycle.fireLifecycleEvent(START_EVENT, null);
    // } catch (Exception e) {
    // e.printStackTrace();
    // }

    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
  }

  public void stop() throws LifecycleException {
    if (!started)
      throw new LifecycleException("SimpleContext has not been started");
    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, null);
    lifecycle.fireLifecycleEvent(STOP_EVENT, null);
    started = false;
    // try {
    // // Stop the Valves in our pipeline (including the basic), if any
    // if (pipeline instanceof Lifecycle) {
    // ((Lifecycle) pipeline).stop();
    // }
    //
    // // Stop our child containers, if any
    // Container children[] = findChildren();
    // for (int i = 0; i < children.length; i++) {
    // if (children[i] instanceof Lifecycle)
    // ((Lifecycle) children[i]).stop();
    // }
    // if ((loader != null) && (loader instanceof Lifecycle)) {
    // ((Lifecycle) loader).stop();
    // }
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
  }

}